package applicationServer.configurationManagement;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

import applicationServer.logic.MediaServerInfo;


public class MediaServersReader {

	
        private String _optionsFileName = null;
        
        private ArrayList<MediaServerInfo> _media = null;
        
        public MediaServersReader (){
                _optionsFileName = new String ("mediaServers.txt");
                _media = new ArrayList<MediaServerInfo>();
                readOptionsFile();
                processOptions();
        }
        
        public MediaServersReader (String n){
                _optionsFileName = new String (n);
                _media = new ArrayList<MediaServerInfo>();
                readOptionsFile();
                processOptions();
        }
        
        private void processOptions() {
        	
        }

        private void readOptionsFile() {
                
            FileInputStream fileIS = null;
            DataInputStream  dataIS = null;
            BufferedReader  bufR = null;
            
            File file = new File(_optionsFileName);

            try {
                fileIS = new FileInputStream(file);

                        // Here BufferedInputStream is added for fast reading.
                        dataIS = new DataInputStream(fileIS);
                        bufR = new BufferedReader(new InputStreamReader(dataIS));

                        // _dataIS.available() returns 0 if the file does not have more lines.
                        while (processElement(bufR.readLine()) != null) {
                        }
                        
                        // dispose all the resources after using them.
                        bufR.close();
                        dataIS.close();
                        fileIS.close();
                                
                } catch (FileNotFoundException e) {
                        e.printStackTrace();
                } catch (IOException e) {
                        e.printStackTrace();
                }
                
        }

        private String processElement (String c){
                if (c != null){
                        c.trim();
                        int pos =-1;
                        int pos1 = -1;
                        int pos2 = -1;
                        MediaServerInfo m = null;
                        if (!c.startsWith("//") && c.endsWith(";") && c.contains(":")){
                            pos1 = c.indexOf("#!=!#");
                            pos2 = c.indexOf("#!=!#",pos1+1);
                            pos = c.indexOf(':',pos2+1);
                            m = new MediaServerInfo(c.substring(pos1+5, pos2).trim(), 
                        				c.substring(pos2+5, pos).trim(), 
                        				c.substring(0, pos1).trim(), 
                        				Integer.parseInt(c.substring(pos+1, c.length()-1).trim())
                        				);
                            _media.add(m);
                        }
                }
                return c;
        }

        public String get_optionsFileName() {
                return _optionsFileName;
        }

		public ArrayList<MediaServerInfo> get_media() {
			// TODO Auto-generated method stub
			return _media;
		}
        
}
